#include "ZenLight.h"

UINT CZenLight::m_LightCounter = 0;

CZenLight::CZenLight()
{
	ZeroMemory(&m_Light, sizeof(D3DLIGHT8));
	m_Light.Type		= D3DLIGHT_POINT;
	m_Light.Diffuse.r	= 1.0f;
	m_Light.Diffuse.g	= 1.0f;
	m_Light.Diffuse.b	= 1.0f;
	m_Light.Attenuation0= 1.0f;
	m_Light.Range		= 100.0f;

	m_ID	= m_LightCounter++;
	m_bIsOn = FALSE;
	m_Position = D3DXVECTOR3(0, 0, 0);	
}

CZenLight::~CZenLight()
{
}

void CZenLight::SetAmbient(float r, float g, float b)
{
	m_Light.Ambient.r = r;
	m_Light.Ambient.g = g;
	m_Light.Ambient.b = b;
}

void CZenLight::SetDiffuse(float r, float g, float b)
{
	m_Light.Diffuse.r = r;
	m_Light.Diffuse.g = g;
	m_Light.Diffuse.b = b;
}

void CZenLight::SetSpecular(float r, float g, float b)
{
	m_Light.Specular.r = r;
	m_Light.Specular.g = g;
	m_Light.Specular.b = b;
}

void CZenLight::SetPosition(float x, float y, float z)
{
	m_Position.x = x;
	m_Position.y = y;
	m_Position.z = z;
}

void CZenLight::GetPosition(float &x, float &y, float &z)
{
	x = m_Position.x;
	y = m_Position.y;
	z = m_Position.z;
}

void CZenLight::Enable(BOOL bEnable)
{
	m_bIsOn = bEnable;
	zen.m_pDevice->LightEnable(m_ID, bEnable);
}

HRESULT CZenLight::Render()
{
	D3DXMATRIX ParentMatrix;
	D3DXVECTOR3 vTempStore; //bugfix
	
	if (m_pParentFrame)
	{
		vTempStore = m_Position; //bugfix
		((CZenFrame*)m_pParentFrame)->GetLocal(ParentMatrix);
		D3DXVec3TransformCoord(&m_Position, &m_Position, &ParentMatrix);
		m_Light.Position = m_Position;
		m_Position = vTempStore;//bugfix
	}
	else
	{
		m_Light.Position = m_Position;

		zen.SetError("Licht wordt gerenderd zonder parentframe!");
	}

	zen.m_pDevice->SetLight(m_ID, &m_Light);

	return S_OK;
}

void CZenLight::SetAmbientLight(D3DCOLOR AmbientColor)
{
	zen.m_pDevice->SetRenderState(D3DRS_AMBIENT, AmbientColor);
}

int CZenLight::GetSize()
{
	return sizeof(*this);
}

void CZenLight::Attenuate(float a0, float a1, float a2)
{
	m_Light.Attenuation0 = a0;
	m_Light.Attenuation1 = a1;
	m_Light.Attenuation2 = a2;
}
